package com.qidianit.mes.pro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.mapper.MaterialMapper;
import com.qidianit.mes.pro.mapper.UnitMapper;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.pojo.MESPage;
import com.qidianit.mes.pro.service.WorkflowProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author: 戴着假发的程序员
 * @ Date: 2025/9/22 - 16:24
 */
@Service
public class WorkflowProductServiceImpl extends ServiceImpl<WorkflowProductMapper, WorkflowProduct> implements WorkflowProductService {

    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;

    @Override
    public Result<MESPage<WorkflowProduct>> selectList(Long workflowId, int pageNum, int pageSize) {
        Page<WorkflowProduct> page = new Page<>(pageNum,pageSize);
        QueryWrapper<WorkflowProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workflow_id",workflowId);
        queryWrapper.eq("delete_flag",0);
        page = baseMapper.selectPage(page,queryWrapper);
        page.getRecords().forEach(item->{
            QueryWrapper<Material> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("code",item.getProductCode());
            Material material = materialMapper.selectOne(wrapper1);
            material.setUnit(unitMapper.selectById(material.getUnitId()));
            item.setProduct(material);
        });
        return Result.success(new MESPage<WorkflowProduct>(page));
    }
}
